<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\CourseSectionQuestionAnswer;
use App\Services\CourseSectionQuestionAnswerService;

use App\SensitiveWords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class CourseSectionQuestionAnswerController extends AdminController
{
    //添加
    public function  add(Request $request){

        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'title' => 'required|string|min:1',
            'body' => 'required|string|min:1',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $bad_word = SensitiveWords::getBadWord($request->body);
        if (!empty($bad_word)) {
            return $this->fail(CodeResponse::PARAM_VALUE_SENSITIVE,'包含敏感词:' . current($bad_word));
        }
        CourseSectionQuestionAnswer::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'title' => 'required|string|min:1',
            'body' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $bad_word = SensitiveWords::getBadWord($request->body);
        if (!empty($bad_word)) {
            return $this->fail(CodeResponse::PARAM_VALUE_SENSITIVE,'包含敏感词:' . current($bad_word));
        }

        $putData = $request->except(['_method','_token']);
        $courseSectionQuestionAnswer = new CourseSectionQuestionAnswer();
        $courseSectionQuestionAnswer->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'course_id' => 'integer|digits_between:1,20|min:1',
            'course_section_id' => 'integer|digits_between:1,20|min:1',
            'users_id' => 'integer|digits_between:1,20|min:1',
            'body' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $body = $request->input('body');

        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');
        $course_id = $request->input('bocourse_iddy');
        $course_section_id = $request->input('course_section_id');
        $users_id = $request->input('users_id');

        $list = CourseSectionQuestionAnswerService::getInstance()->list($sort,$order, $body, $course_id, $course_section_id, $users_id);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $answer = CourseSectionQuestionAnswer::find($request->id);
        $answer->delete();
        return $this->success();
    }

    //增加浏览量
    public function  addPV(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $answer = CourseSectionQuestionAnswer::find($request->id);
        $pv = is_null($answer->pv) ? 0 : $answer->pv+1;
        $answer= new CourseSectionQuestionAnswer();
        $answer->find($request->id)->update(['pv'=>$pv]);
        return $this->success();
    }
}
